Page({

  /**
   * 页面的初始数据
   */
  data: {
    recording: false,
    recordList: []
  },

  recordStart(event) {
    const recordManager = wx.getRecorderManager()
    recordManager.start()
    recordManager.onStop(res => {
      console.log(res)
      this.setData({
        recordList: this.data.recordList.concat({
          text: '他是准备起飞前的准备工作',
          score: 8,
          comment: '语速过慢',
          timeLen: Math.ceil(res.duration / 1000),
          filePath: res.tempFilePath,
          audioContext: null,
          playing: false
        })
      })
    })
    this.setData({
      recording: true
    })
  },

  recordEnd(event) {
    const recordManager = wx.getRecorderManager()
    recordManager.stop()
    this.setData({
      recording: false
    })
  },

  playRecord(event) {
    const { src } = event.currentTarget.dataset
    const item = this.data.recordList.find(_ => {
      return _.filePath === src
    })
    const index = this.data.recordList.indexOf(item)

    if (item.audioContext) {
      if (item.playing) {
        item.playing = false
      } else {
        this.data.recordList.forEach(_ => {
          _.audioContext && _.audioContext.pause()
          _.playing = false
        })
        item.audioContext.play()
        item.playing = true
      }
    } else {
      const audioContext = wx.createInnerAudioContext({
        useWebAudioImplement: true
      })
      audioContext.src = src
      audioContext.play()
      audioContext.onEnded(() => {
        
      })
      // item.audioContext = audioContext
    }

    // this.setData({
    //   [`recordList.[${index}]`]: item
    // })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})